package com.partybuilding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.partybuilding.domain.ResponseResult;
import com.partybuilding.domain.entity.Partytable;
import com.partybuilding.domain.entity.Usertable;
import com.partybuilding.domain.vo.PartyCheckVo;
import com.partybuilding.domain.vo.UserCheckVo;
import com.partybuilding.mapper.PartytableMapper;
import com.partybuilding.service.PartytableService;
import com.partybuilding.service.UsertableService;
import com.partybuilding.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 支部信息表(Partytable)表服务实现类
 *
 * @author makejava
 * @since 2023-11-02 01:32:59
 */
@Service("partytableService")
public class PartytableServiceImpl extends ServiceImpl<PartytableMapper, Partytable> implements PartytableService {

    @Autowired
    private UsertableService usertableService;
    @Autowired
    private PartytableService partytableService;

    @Override
    public ResponseResult getAllUserByPartyId(String partyId) {
//        通过支部id查询支部成员
        LambdaQueryWrapper<Usertable> usertableWrapper=new LambdaQueryWrapper<>();
        //若id长度不为13说明不是基层党支部
        if(partyId.length()!=15)
        {
            //从左侧开始匹配,部分符合的都是下属党员
            usertableWrapper.likeRight(Usertable::getPd_branchesid,partyId);
        }
        else {
            usertableWrapper.eq(Usertable::getPd_branchesid,partyId);
        }
        List<Usertable> usertableList=usertableService.list(usertableWrapper);
        List<UserCheckVo> userCheckVos=BeanCopyUtils.copyBeanList(usertableList,UserCheckVo.class);
        return ResponseResult.okResult(userCheckVos);
    }

    @Override
    public ResponseResult getAllUserByPartyName(String partyName) {

        //通过支部名称查询支部id
        LambdaQueryWrapper<Partytable> partytableLambdaQueryWrapper=new LambdaQueryWrapper<>();
        partytableLambdaQueryWrapper.eq(Partytable::getBranches_name,partyName);
        Partytable partytable=partytableService.getOne(partytableLambdaQueryWrapper);

        String partyId=partytable.getBranches_id();
        LambdaQueryWrapper<Usertable> usertableWrapper=new LambdaQueryWrapper<>();
        //若id长度不为13说明不是基层党支部
        if(partyId.length()!=15)
        {
            //从左侧开始匹配,部分符合的都是下属党员
            usertableWrapper.likeRight(Usertable::getPd_branchesid,partyId);
        }
        else {
            usertableWrapper.eq(Usertable::getPd_branchesid,partyId);
        }
        List<Usertable> usertableList=usertableService.list(usertableWrapper);
        List<UserCheckVo> userCheckVos=BeanCopyUtils.copyBeanList(usertableList,UserCheckVo.class);
        return ResponseResult.okResult(userCheckVos);
    }

    @Override
    public ResponseResult getPartyByPartyName(String partyName) {
        LambdaQueryWrapper<Partytable> partytableLambdaQueryWrapper=new LambdaQueryWrapper<>();
        partytableLambdaQueryWrapper.eq(Partytable::getBranches_name,partyName);
        Partytable partytable=getOne(partytableLambdaQueryWrapper);
        PartyCheckVo PartyCheckVo = BeanCopyUtils.copyBean(partytable, com.partybuilding.domain.vo.PartyCheckVo.class);
        return ResponseResult.okResult(PartyCheckVo);
    }
}

